import { Component, Input, Output, EventEmitter } from '@angular/core';
import { Camera, CameraOptions } from "@ionic-native/camera";
import { ModalController } from 'ionic-angular';

// service
import { ActionSheetService } from '../../providers/utils/actionSheet.service';
import * as Constant from '../../constant/constant';

// 图片查看
import { GalleryModal } from 'ionic-gallery-modal'
/**
 * 相机组件
 */
@Component({
  selector: 'office-camera',
  templateUrl: 'office-camera.html'
})
export class OfficeCameraComponent {

  // 图片路径
  @Input() imagePaths: any;
  // 相机背景
  @Input() cameraBackGround: string = 'assets/img/common/camera.svg';
  // 初始状态
  @Input() initStatus: string;
  // 选择的图片
  @Output() imagesSelected = new EventEmitter<any>();
  // 重置选择
  @Output() resetSelected = new EventEmitter<any>();
  constructor(
    private actionSheetCtrl: ActionSheetService,
    public modalCtrl: ModalController,
    private camera: Camera,
  )
  {}

  addImg(){
    let random = Math.round(Math.random()*2+1);
    this.imagePaths= {url:`assets/img/borrow-money/test0${random}.png`,type:''};
    this.emitImagesData();
  }
  /*** 打开相机
   * */
  executeCamera(): void {
    let options: CameraOptions = {
      sourceType: this.camera.PictureSourceType.CAMERA,
      destinationType: this.camera.DestinationType.DATA_URL,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true,
      quality: 50,
      allowEdit: true,
      targetWidth: 720,
      encodingType: this.camera.EncodingType.JPEG
    };
    this.camera.getPicture(options).then((imageURI) => {
      this.imagePaths= {url:Constant.BASE4_PREFIX+ imageURI,type:''};
      this.emitImagesData();
    }, (error) => {
      console.log(error);
    });
  }

  /*** 选择操作
  * */
  executePhoto()
  {
    debugger;
    // 仅仅展示时
    if(this.initStatus === Constant.ONLY_SHOW)
    {
      this.executePhotoView([this.imagePaths],'0');
      return;
    }

    this.actionSheetCtrl.createIdentityCard().then(data=>{
      if(data.type === Constant.LOOK)
      {
        this.executePhotoView([this.imagePaths],'0');
        return;
      }
      if(data.type === Constant.RESET)
      {
        this.imagePaths= {url: null};
        this.emitResetSelected();
        return;
      }
    });
  }

  // 发送更新图片数组
  emitImagesData() {
    this.imagesSelected.emit({
      value: this.imagePaths
    });
  }

  // 发送更新图片数组
  emitResetSelected() {
    this.resetSelected.emit();
  }

  /***  打开图片预览
   * @param photos 预览数组
   * @param index 数组index
   */
  executePhotoView(photos: Array<any>,index: string)
  {
    this.createModal(GalleryModal,{
      photos: photos,
      initialSlide: index
    })
  }

  /*** 创建modal
   * @param page 要到的位置
   * @param data 传输参数, 不传默认空
   * */
  private createModal(page: any,data: any= {})
  {
    const modal = this.modalCtrl.create(page, data,{
      showBackdrop: false,
      enableBackdropDismiss: false,
      enterAnimation: Constant.MODAL_SCALE_UP_ENTER,
      leaveAnimation: Constant.MODAL_SCALE_UP_LEAVE,
    });
    modal.present();
  }

}
